package cn.wolfcode.wolf2w.controller;

import cn.wolfcode.wolf2w.annotation.RequireLogin;
import cn.wolfcode.wolf2w.annotation.UserParam;
import cn.wolfcode.wolf2w.domain.Strategy;
import cn.wolfcode.wolf2w.domain.StrategyComment;
import cn.wolfcode.wolf2w.domain.StrategyTheme;
import cn.wolfcode.wolf2w.domain.UserInfo;
import cn.wolfcode.wolf2w.query.StrategyCommentQueryObject;
import cn.wolfcode.wolf2w.query.StrategyQueryObject;
import cn.wolfcode.wolf2w.redis.service.IStatisVORedisService;
import cn.wolfcode.wolf2w.service.IStrategyCommentService;
import cn.wolfcode.wolf2w.service.IStrategyService;
import cn.wolfcode.wolf2w.service.IStrategyThemeService;
import cn.wolfcode.wolf2w.util.JsonResult;
import cn.wolfcode.wolf2w.vo.StrategyStatisVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("strategies")
public class StrategyController {

    @Autowired
    private IStrategyService strategyService;

    @Autowired
    private IStrategyThemeService strategyThemeService;

    @Autowired
    private IStrategyCommentService strategyCommentService;

    @Autowired
    private IStatisVORedisService statisVORedisService;

    @GetMapping("/getStrategiesByTop3")
    public Object getStrategiesByTop3(String destId) {
        List<Strategy> strategyies = strategyService.getStrategiesTop3ByDestId(destId);
        return JsonResult.success(strategyies);
    }

    @GetMapping("/getStrategyById")
    public Object getStrategyById(String id) {

        Strategy strategy = strategyService.get(id);

        statisVORedisService.viewNumIncrease(id);

        return JsonResult.success(strategy);
    }

    @GetMapping("/query")
    public Object query(StrategyQueryObject qo) {
        qo.setState(1);
        Page<Strategy> strategies = strategyService.query(qo);
        return JsonResult.success(strategies);
    }

    @GetMapping("/themes")
    public Object themes() {
        List<StrategyTheme> strategyThemes = strategyThemeService.list();
        return JsonResult.success(strategyThemes);
    }

    @GetMapping("/comments")
    public Object comments(StrategyCommentQueryObject qo) {
        return JsonResult.success(strategyCommentService.query(qo));
    }

    @RequireLogin
    @PostMapping("/addComment")
    public Object addComment(StrategyComment strategyComment, @UserParam UserInfo userInfo) {
        strategyCommentService.addComment(strategyComment, userInfo);
        statisVORedisService.replyNumIncrease(strategyComment.getStrategyId());
        return JsonResult.success();
    }


    @RequireLogin
    @PostMapping("/commentThumb")
    public Object commentThumb(String cid, @UserParam UserInfo userInfo) {
        strategyCommentService.commentThumb(cid, userInfo);
        return JsonResult.success();
    }

    @GetMapping("/statisVo")
    public Object statisVo(String sid) {
        StrategyStatisVo vo = strategyService.getStrategyStatisVo(sid);
        return JsonResult.success(vo);
    }

    @RequireLogin
    @PostMapping("/favor")
    public Object favor(String sid, @UserParam UserInfo userInfo) {
        boolean ret = statisVORedisService.favorNumIncrease(sid, userInfo);
        return JsonResult.success(ret);
    }

    @RequireLogin
    @PostMapping("/strategyThumbup")
    public Object strategyThumbup(String sid, @UserParam UserInfo userInfo) {
        boolean ret = statisVORedisService.isThumbupToday(sid, userInfo);
        return JsonResult.success(ret);
    }

}
